<?php
class Hotel{
	/*取酒店列表*/
	public function getHotelList(){
		$t=new db_recordset(NULL,"zyw_hotel_main");
		$sql="select * from zyw_hotel_main";
		$t->query($sql,-1);
		$hotelList=$t->getRecords();
		return $hotelList;
	}
	/*增加酒店*/
	public function addHotel($hotelParam){
		$t=new db_table(NULL,"zyw_hotel_main");
		$t->insert();
		$t->hotelname=$hotelParam['name'];
		$t->sid=$hotelParam['sid'];
		$t->hotellevel=$hotelParam['hotellevel'];
		$t->hoteldetail=$hotelParam['hoteldetail'];
		$t->preday=$hotelParam['preday'];
		$t->hotelfeature=$hotelParam['hotelfeature'];
		$t->hoteladdr=$hotelParam['addr'];
		$t->evaluation=$hotelParam['evaluation'];
		$t->recommend=$hotelParam['recommend'];
		$t->pv=0;
		$t->post();
		
		$hotelid=$t->hotelid;
		$this->saveHotelDetail($hotelParam['hoteldetail'],$hotelid);
		$arealist=array();
		if($hotelParam['district']!=-1)	$arealist[]=$hotelParam['district'];
		if($hotelParam['city']!=-1)	$arealist[]=$hotelParam['city'];
		if($hotelParam['province']!=-1)	$arealist[]=$hotelParam['province'];
		if($hotelParam['area']!=-1)	$arealist[]=$hotelParam['area'];
		if($hotelParam['foreign']!=-1)	$arealist[]=$hotelParam['foreign'];
				
		$ret=$this->addRegion2Hotel($arealist,$hotelid);
	}
	private function addRegion2Hotel($areaArr,$hotelid){
			$t=new db_table(NULL,"zyw_scenic2region");
			foreach ($areaArr as $area){
				$t->insert();
				$t->sid=$hotelid;
				$t->rid=$area;
				$t->type='1';//增加酒店
				$t->post();
				//$this->addRegionRef($area);
			}
			return true;
		}
	/**
	 * @name 修改酒店信息
	*/
	public function editHotelById($hotelid,$hotelParam){
		$t=new db_table(NULL,"zyw_scenic2region");
		$t->locate($hotelid,-1);
		if($t->record_count()!=1) return false;
		$t->hotelname=$hotelParam['name'];
		$t->sid=$hotelParam['sid'];
		$t->hotellevel=$hotelParam['hotellevel'];
		$t->hoteldetail=$hotelParam['hoteldetail'];
		$t->hotelfeature=$hotelParam['hotelfeature'];
		$t->hoteladdr=$hotelParam['addr'];
		$t->evaluation=$hotelParam['evaluation'];
		$t->recommend=$hotelParam['recommend'];
		$t->post();
		
		$arealist=array();
		if($hotelParam['district']!=-1)	$arealist[]=$hotelParam['district'];
		if($hotelParam['city']!=-1)	$arealist[]=$hotelParam['city'];
		if($hotelParam['province']!=-1)	$arealist[]=$hotelParam['province'];
		if($hotelParam['area']!=-1)	$arealist[]=$hotelParam['area'];
		if($hotelParam['foreign']!=-1)	$arealist[]=$hotelParam['foreign'];
		
		$this->delRegionByHotelId($hotelid);
		$ret=$this->addRegion2Hotel($arealist,$hotelid);
	}
	/*取地区信息*/
	public function getRegionByLevel($level=-1){
		$t=new db_recordset(NULL,"zyw_scenic_region");
		if($level==-1)	$sql="select * from zyw_scenic_region order by rlevel";
		else	$sql="select * from zyw_scenic_region where rlevel=$level";
		$t->query($sql,-1);
		$rl=$t->getRecords();
		return $rl;
	}
	/*取景区列表*/
	public function getScenicList(){
		$t=new db_recordset(NULL,'zyw_scenic');
		$sql="select * from zyw_scenic";
		$t->query($sql,-1);
		$scelist=$t->getRecords();
		return $scelist;
	}
	/*取酒店信息*/
	public function getHotelById($hotelid){
		$t=new db_recordset(NULL,"zyw_hotel_main");
		$sql="select * from zyw_hotel_main where hotelid=$hotelid";
		$t->query($sql,-1);
		$hotel=$t->getRecords();
		$content=$this->getHotelDetail($hotelid);
		$hotel[0]['htmlcontent']=$content;
		//print_r($hotel);
		return $hotel[0];
	}
	/*删除酒店*/
	public function delHotel($hotelid){
		$t=new db_recordset(NULL,"zyw_hotel_main");
		$sql="delete from zyw_hotel_main where hotelid=$hotelid";
		$t->exec($sql,-1);
		$t->exec("delete from zyw_hotel_detail where hotelid=".$hotelid,-1);
		$this->delHotelDetail($hotelid);
	}
	private function delRegionByHotelId($hotelid){
		$t=new db_table(NULL,"zyw_scenic2region");
		$sql="delete from zyw_scenic2region where hotelid=$hotelid";
		$t->exec($sql,-1);
		return true;
	}
	public function getRegion2Hotel($hotelid){
		$t=new db_recordset(NULL,"zyw_scenic2region");
		$sql="select a.*,b.rname,b.rlevel from zyw_scenic2region a,zyw_scenic_region b where type=1 and a.rid=b.rid and sid=$hotelid";
		$t->query($sql,-1);
		$reg2hotel=$t->getRecords();
		return $reg2hotel;
	}
	public function getRoomTypeByHotel($hotelid){
		$t=new db_recordset(NULL,"zyw_hotel_detail");
		$sql="select * from zyw_hotel_detail where hotelid=$hotelid";
		$t->query($sql,-1);
		$roomList=$t->getRecords();
		return $roomList;
	}
	public function addRoomType($paramArr){
		$t=new db_table(NULL,"zyw_hotel_detail");
		$t->insert();
		$t->hotelid=$paramArr['hotelid'];
		$t->roomtype=$paramArr['roomtype'];
		$t->pricetype=$paramArr['pricetype'];
		$t->price=$paramArr['price'];
		$t->roomnum=$paramArr['roomnum'];
		$t->memo=$paramArr['memo'];
		$t->post();
		return true;
	}
	public function delRoom($id){
		$t=new db_recordset(NULL,"zyw_hotel_detail");
		$sql="delete from zyw_hotel_detail where id=$id";
		$t->exec($sql,-1);
		return true;
	}
	public function getRoomById($id){
		$t=new db_recordset(NULL,"zyw_hotel_detail");
		$sql="select * from zyw_hotel_detail where id=$id";
		$t->query($sql,-1);
		$room=$t->getRecords();
		return $room[0];
	}
	public function editRoom($id,$paramArr){
		$t=new db_table(NULL,"zyw_hotel_detail");
		$t->locate($id,-1);
		
		if($t->record_count()!=1) return false;
		$t->roomtype=$paramArr['roomtype'];
		$t->roomnum=$paramArr['roomnum'];
		$t->price=$paramArr['price'];
		$t->pricetype=$paramArr['pricetype'];
		$t->memo=$paramArr['memo'];
		$t->post();
		return true;
	}
	private  function saveHotelDetail($detail,$hotelid){
		$filename="/tmp/".$hotelid.".html";
		 if (!$handle = fopen($filename, 'a')) {
         echo "不能打开文件 $filename";
	        return false;
   		}
   		// 将$somecontent写入到我们打开的文件中。
	    if (fwrite($handle, $detail) === FALSE) {
	        echo "不能写入到文件 $filename";
	       return false;
	    }
	    fclose($handle);
	    return true;
	}
	private function getHotelDetail($hotelid){
		// get contents of a file into a string
		$filename = "/tmp/".$hotelid.".html";
		$handle = fopen($filename, "rb");
		$contents = fread($handle, filesize ($filename));
		$contents=stripcslashes($contents);
		return $contents;
	}
	private function delHotelDetail($hotelid){
		$filename="/tmp/".$hotelid.".html";
		return unlink($filename);
	}
}
?>